<template>
  <div class="login-container">
    <div class="wrap-login">
      <div class="login-box">
        <!-- 头像区域 -->
        <div class="avatar-box">
          <span>用户权限管理系统</span>
        </div>
        <!-- 表单区域 -->
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="80px"
          class="form-login"
          hide-required-asterisk
        >
          <!-- 用户名 -->
          <el-form-item label="用户名：" prop="userName">
            <el-input v-model="ruleForm.userName" placeholder="请输入用户名" clearable></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item label="密码：" prop="password">
            <el-input
              type="password"
              v-model="ruleForm.password"
              placeholder="请输入密码"
              clearable
            ></el-input>
          </el-form-item>
          <!-- 登录按钮 -->
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { loginAPI } from '../api/loginAPI'
import { Message } from 'element-ui'

export default {
  name: 'MyLogin',
  data() {
    return {
      ruleForm: {
        userName: '',
        password: ''
      },
      rules: {
        userName: [
          { required: true, message: '请输入您的用户名', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 18, message: '密码长度在 6 到 18 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate(async valid => {
        try {
          if (valid) {
            const result = await loginAPI(this.ruleForm)
            if (result.code == 200) {
              Message.success('登录成功')
              window.localStorage.setItem('token', result.data.token)
              window.localStorage.setItem('username', this.ruleForm.userName)
              this.$router.push('/home')
            } else {
              Message.warning('用户名或密码不正确')
            }
          }
        } catch (err) {
          Message.error('登录失败')
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  background-color: #35495e;
  height: 100%;
  background: url(../utils/images/bg.jpeg) no-repeat center center;
  background-size: 100% 100%;
}
// .wrap-login {
//   width: 800px;
//   height: 600px;
//   border-radius: 3px;
//   position: relative;
//   left: 50%;
//   top: 50%;
//   transform: translate(-50%, -50%);
// }
.avatar-box {
  position: absolute;
  width: 100%;
  height: 80px;
  line-height: 80px;
  text-align: center;
  font-size: 24px;

  // background-color: skyblue;
}
.login-box {
  width: 400px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border-radius: 3px;
  box-shadow: 5px 5px 10px 10px #ccc;
}

.form-login {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>
